package com.tongyu.customappbar.custom;


import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.tongyu.customappbar.R;

import java.util.ArrayList;
import java.util.List;

/*
 *@Author TongYu
 *@Date: 2021/8/28
 *@Describe:
 */
public class MyAppBar extends LinearLayout implements View.OnClickListener {
    private List<MyItemBean> list;
    LinearLayout linearLayout;
    LayoutParams layoutParams;

    public MyAppBar(Context context) {
        super(context);
        init();
    }

    public MyAppBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyAppBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void init() {
        list = new ArrayList<>();
        setLayoutParams(new ViewGroup.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)));
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.app_bar, this);
        linearLayout = inflate.findViewById(R.id.app_bar);
        layoutParams = new LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.weight = 1;
        layoutParams.gravity = Gravity.CENTER;
    }

    public MyAppBar addItem(MyItemBean myItemBean) {
        list.add(myItemBean);
        return this;
    }

    public void build() {
        for (int i = 0; i < list.size(); i++) {
            MyItemBean myItemBean = list.get(i);
            if (myItemBean.getBar_title() != null) {
                TextView textView = new TextView(getContext());
                textView.setText(myItemBean.getBar_title());
                textView.setTextSize(18);
                textView.setTextColor(Color.GRAY);
                textView.setGravity(Gravity.CENTER);
                textView.setLayoutParams(layoutParams);
                textView.setTag(R.id.barId, i);
                textView.setOnClickListener(this);
                linearLayout.addView(textView);
            } else {
                ImageView imageView = new ImageView(getContext());
                imageView.setImageResource(myItemBean.getBar_img());
                imageView.setLayoutParams(layoutParams);
                imageView.setTag(R.id.barId, i);
                imageView.setOnClickListener(this);
                linearLayout.addView(imageView);
            }
        }
    }

    public void selected(int position) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getBar_title() != null) {
                if (i == position) {
                    TextView textView = (TextView) linearLayout.getChildAt(i);
                    textView.setTextColor(Color.BLACK);
                    textView.setTextSize(20);
                } else {
                    TextView textView = (TextView) linearLayout.getChildAt(i);
                    textView.setTextColor(Color.GRAY);
                    textView.setTextSize(18);
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        int tag = (int) v.getTag(R.id.barId);
        myListener.select_clicked(tag);
    }

    public interface MyListener {
        void select_clicked(int position);
    }

    private MyListener myListener;

    public void setMyListener(MyListener myListener) {
        this.myListener = myListener;
    }
}
